package chapter5;

import basic.lessons.TextIO;

public class StatCalc {
	   private int count;   // Number of numbers that have been entered.
	   private double sum;  // The sum of all the items that have been entered.
	   private double squareSum;  // The sum of the squares of all the items.
	   private double maxNumber;
	   private double minNumber;
	   /**
	    * Add a number to the dataset.  The statistics will be computed for all
	    * the numbers that have been added to the dataset using this method.
	    */
	   public void enter(double num) {
	      count++;
	      sum += num;
	      squareSum += num*num;
	   }
	   
	   /**
	    * Return the number of items that have been entered into the dataset.
	    */
	   public int getCount() {
	      return count;
	   }
	   
	   /**
	    * Return the sum of all the numbers that have been entered.
	    */
	   public double getSum() {
	      return sum;
	   }
	   
	   /**
	    * Return the average of all the items that have been entered.
	    * The return value is Double.NaN if no numbers have been entered.
	    */
	   public double getMean() {
	      return sum / count;  
	   }
	   public double getMax() {
		      return maxNumber;  
		   }
	   
	   
	   /**
	    * Return the standard deviation of all the items that have been entered.
	    * The return value is Double.NaN if no numbers have been entered.
	    */
	   public double getStandardDeviation() {  
	      double mean = getMean();
	      return Math.sqrt( squareSum/count - mean*mean );
	   }
	   public static void main (String[] args){
                                              
		StatCalc sc = new StatCalc();
		double newValue = 1;
		while (newValue != 0) {
			 TextIO.putln("Please, enter your number and press \"Enter\"");
			 TextIO.putln("Type '0' to finish entering numbers");
			newValue = TextIO.getlnDouble();

			if (sc.minNumber > newValue)
				sc.minNumber = newValue;
			if (sc.maxNumber < newValue)
				sc.maxNumber = newValue;

			sc.enter(newValue);

		}
		 TextIO.putln();
		 TextIO.putln("Total numbers entered are " + sc.getCount());
		 TextIO.putln("The sum of numbers is " + sc.getSum());
		 TextIO.putln("The mean of numbers is " + sc.getMean());
		 TextIO.putln("Standard Deviation is " + sc.getStandardDeviation());
		 TextIO.putln("Max Number is " + sc.maxNumber);
		 TextIO.putln("Min Number is " + sc.minNumber);
	}
	   
	      
}  // end class StatCalc